import os
from core.database import OpenGaussManager,DatabaseManager
import pandas as pd

# in jupyter notebook current_path get with dir '.'
# current_path = os.path.abspath('.')
current_path = os.path.dirname(os.path.realpath(__file__))

if __name__ == "__main__":
    try:
        #manager=OpenGaussManager(current_path+"/config.yaml")
        manager=DatabaseManager(current_path+"/config.yaml")
        manager.set_provider('opengauss')
        conn_str=manager.get_connstr()
        # 从CSV文件读取鸢尾花数据集
        #train = pd.read_csv(current_path+'/train.csv')
        # 从database表iris_train中读取鸢尾花数据集
        train_table="iris_train"
        print(conn_str)
        try:
            conn = manager.connect()
            # 使用连接执行查询
            with conn.cursor() as cursor:
                cursor.execute("SELECT version()")
                print("PostgreSQL版本:", cursor.fetchone())
            print("\n连接成功！")
            engine=manager.create_engine()
            train=pd.read_sql('''SELECT * FROM  iris_train;''', con = engine,index_col="id")
            #train.to_sql(train_table, engine, if_exists='replace', index=False)
        except ConnectionError as e:
            print(f"\n连接失败: {e}")
        finally:
            manager.close()
            print("\n连接已关闭")
    except ValueError as e:
        print(f"配置错误: {e}")
    except Exception as e:
        print(f"意外错误: {e}")